package com.blank.gpstest;

import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.GnssMeasurementsEvent;
import android.location.GnssStatus;
import android.location.GpsStatus;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.blank.gps.GpsListener;
import com.blank.gps.MyLocationObserver;
import com.blank.gps.MyLocationService;

import java.util.List;
import java.util.Locale;

public class GPSActivity extends AppCompatActivity implements GpsListener {

    private static final String TAG = "GPSActivity";
    private TextView mTextView, tvAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gpsactivity);
        mTextView = findViewById(R.id.tv);
        tvAddress = findViewById(R.id.tv_address);
    }

    @Override
    protected void onResume() {
        super.onResume();
        startGps();
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopGps();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopGps();
    }

    /**
     * 开启服务
     */
    private void startGps() {
        startService(new Intent(this, MyLocationService.class));
        MyLocationObserver.addListener(this);
    }

    /**
     * 获取地址信息:城市、街道等信息
     *
     * @param location
     * @return
     */
    private List<Address> getAddress(Location location) {
        List<Address> result = null;
        try {
            if (location != null) {
                Geocoder gc = new Geocoder(this, Locale.getDefault());
                Log.d(TAG, "Geocoder.isPresent : " + Geocoder.isPresent());
                result = gc.getFromLocation(location.getLatitude(),
                        location.getLongitude(), 1);
                Log.d(TAG, "获取地址信息：" + result);
                tvAddress.setText(result.toString());
                for (int i = 0; i < result.size(); i++) {
                    Log.d("ceshi", "position:" + i + " " + result.get(i).getAdminArea() + "" + result.get(i).getLocality()
                    +result.get(i).getThoroughfare()+""+result.get(i).getAddressLine(1));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 停止服务
     */
    private void stopGps() {
        stopService(new Intent(this, MyLocationService.class));
    }

    @Override
    public void onLocationChanged(@NonNull Location location) {
        getAddress(location);
        mTextView.setText(location.toString());
    }

    @Override
    public void gpsStart() {

    }

    @Override
    public void gpsStop() {

    }

    @Override
    public void onGpsStatusChanged(int event, GpsStatus status) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onGnssFirstFix(int ttffMillis) {

    }

    @Override
    public void onSatelliteStatusChanged(GnssStatus status) {

    }

    @Override
    public void onGnssStarted() {

    }

    @Override
    public void onGnssStopped() {

    }

    @Override
    public void onGnssMeasurementsReceived(GnssMeasurementsEvent event) {

    }

    @Override
    public void onOrientationChanged(double orientation, double tilt) {

    }

    @Override
    public void onNmeaMessage(String message, long timestamp) {

    }

    /**
     * Called when a GNSS fix is acquired, including on first fix
     */
    @Override
    public void onGnssFixAcquired() {

    }

    /**
     * Called when a GNSS fix is lost, following initial acquisition (this is not called on startup
     * prior to a fix initially being acquired)
     * {@link GpsListener}
     */
    @Override
    public void onGnssFixLost() {

    }
}